/*
 * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.mrd.gtimpframe.action;

import com.mrd.base.Jqgrid;
import com.mrd.framework.config.web.json.Result;
import com.mrd.gtimp.base.service.UserBiz;
import com.mrd.gtimpframe.entity.DeclaOperationRecord;
import com.mrd.gtimpframe.service.DeclaOperationRecordService;
import com.mrd.util.LoginUtil;
import com.mrd.util.StringUtils;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by dengdc on 2017/11/17.
 */
@Controller
@RequestMapping("/business/declaOpRec")
public class DeclaOperationRecordController {
    @Resource
    private UserBiz userBiz;
    @Resource
    private DeclaOperationRecordService declaOperationRecordService;

    /**
     * 列表管理页面
     */
    @RequestMapping("/listPage")
    public String listPage(Model model) {
        try {
            //是否是供应商，用于区分显示和限制可操作内容
            boolean isSup = userBiz.currentUserContainOneOfRoles("供应商管理员", "报关公司");
            //是否是客户，用于区分显示和限制可操作内容
            boolean isCust = userBiz.currentUserContainOneOfRoles("客户");
            model.addAttribute("isSup", isSup);
            model.addAttribute("isCust", isCust);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //转到listPage.ftl
        return "/operationRecordRec/listPage";
    }

    /**
     * 查看页面
     *
     * @param did 报关单ID
     */
    @RequestMapping("/viewPage")
    public String viewPage(String did, Model model) {
        model.addAttribute("did", did);
        //是否是供应商，用于区分显示和限制可操作内容
        boolean isSup = userBiz.currentUserContainOneOfRoles("供应商管理员", "报关公司");
        model.addAttribute("isSup", isSup);
        //转到viewPage.ftl
        return "/operationRecordRec/viewPage";
    }

    /**
     * 新增页面
     *
     * @param did 报关单ID
     */
    @RequestMapping("/addPage")
    public String addPage(String did, Model model) {
        model.addAttribute("did", did);
        //转到addPage.ftl
        return "/operationRecordRec/addPage";
    }


    /**
     * 列表页面表格数据
     *
     * @param jqgrid 分页、查询条件、排序等信息的封装对象
     */
    @RequestMapping("/listPageData")
    @ResponseBody
    public Page listPageData(Jqgrid jqgrid) {
        return declaOperationRecordService.getGridData(jqgrid);
    }

    /**
     * 查看业务操作记录界面，历史操作记录
     *
     * @param jqgrid 分页、排序、查询条件
     * @param did    报关单ID
     */
    @RequestMapping("/hisOpRecGridData")
    @ResponseBody
    public Page hisOpRecGridData(Jqgrid jqgrid, String did) {
        return declaOperationRecordService.hisOpRecGridData(jqgrid, did);
    }

    /**
     * 查看操作记录界面，报关单资料信息
     *
     * @param did 报关单ID
     */
    @RequestMapping("/recDeclarationInfoData")
    @ResponseBody
    public Map recDeclarationInfoData(String did) {
        return declaOperationRecordService.recDeclarationInfoFormData(did);
    }

    /**
     * 保存操作记录
     *
     * @param record 业务操作记录
     * @return 保存结果
     */
    @RequestMapping("/saveRecord")
    @ResponseBody
    public Result saveRecord(DeclaOperationRecord record) {
        return declaOperationRecordService.saveRecord(record);
    }

    /**
     * 下载单证发放记录
     */
    @RequestMapping("/downloadDocHoRecord")
    @ResponseBody
    public void downloadDocHoRecord(HttpServletResponse response, HttpServletRequest request) throws Exception {
        Map<String, String[]> params = request.getParameterMap();
        if (params.size() == 0) {
            throw new RuntimeException("请输入正确的参数");
        }
        Map<String, Object> varis = new HashMap<>();
        for (Object key : params.keySet()) {
            String[] data = params.get(key.toString());
            String data0 = data[0];
            if (StringUtils.isNotEmpty(data0)) {
                varis.put(key.toString(), data0);
            }
        }
        declaOperationRecordService.downloadDocHoRecord(request, response, varis);
    }

    /**
     * 业务操作效率查询页面
     */
    @RequestMapping("/operationalEfficiencyPage")
    public String operationalEfficiencyPage() {
        return "gtimpframe/operationalefficiency/OperationalEfficiency";
    }

    /**
     * 业务操作效率查询
     */
    @RequestMapping("/getOperationalEfficiency")
    @ResponseBody
    public void getOperationalEfficiency(Jqgrid jqgrid, HttpServletResponse response) {
        declaOperationRecordService.getOperationalEfficiency(jqgrid, response);
    }

    /**
     * 业务操作效率记录下载
     */
    @RequestMapping("/downloadOperationalEfficiency")
    @ResponseBody
    public void downloadOperationalEfficiency(String whereCase, HttpServletRequest request, HttpServletResponse response) throws Exception {
        if (StringUtils.isEmpty(whereCase)) {
            throw new RuntimeException("请输入正确的参数");
        }
        declaOperationRecordService.downloadOperationalEfficiency(whereCase, request, response);
    }

    /**
     * 业务操作量统计页面
     */
    @RequestMapping("/operationStatisticsPage")
    public String operationStatisticsPage() {
        return "gtimpframe/operationalefficiency/OperationStatistics";
    }

    /**
     * 业务操作量统计
     */
    @RequestMapping("/getOperationStatistics")
    @ResponseBody
    public void getOperationStatistics(Jqgrid jqgrid, HttpServletResponse response) {
        declaOperationRecordService.getOperationStatistics(jqgrid, response);
    }

    /**
     * 业务操作量统计记录下载
     */
    @RequestMapping("/downloadOperationStatistics")
    @ResponseBody
    public void downloadOperationStatistics(String declarationType, String whereCase, HttpServletRequest request, HttpServletResponse response) throws Exception {
        if (StringUtils.isEmpty(whereCase)) {
            throw new RuntimeException("请输入正确的参数");
        }
        declaOperationRecordService.downloadOperationStatistics(declarationType, whereCase, request, response);
    }

    /**
     * 某项操作的登记次数
     * @param did 报关单ID
     * @param recItemCode 操作项目
     */
    @RequestMapping("/getOpCount")
    @ResponseBody
    public Result getOpCount(String did, String recItemCode){
        Integer count = declaOperationRecordService.countOperationRecord(did, recItemCode);
        if(count == null){
            return Result.newFailInstance("无法统计登记次数");
        }
        return Result.newSuccessInstance(String.valueOf(count));
    }

    /**
     * 导出单证发放记录界面
     */
    @RequestMapping("/downloadHoDocPage")
    public String downloadHoDocPage(){
       return "/operationRecordRec/downloadDocPage";
    }
}
